package com.example.demo.controller;

import com.example.demo.domain.User;
import com.example.demo.service.interfaces.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "UserController",description = "用户模块接口")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/add/{id}/{name}/{address}",method = RequestMethod.GET)
    public User addUser(@PathVariable int id,@PathVariable String name,@PathVariable String address)
    {
        User user=new User();
        user.setId(id);
        user.setName(name);
        user.setAddress(address);

        userService.saveUser(user);
        return user;
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public void  deleteUser(@PathVariable int id)
    {
        userService.delete(id);
    }

    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public List<User> getUsers(){
        List<User> users=userService.findAll();

        return users;
    }

    /**
     * @Author chenhegang
     * @Date 2018/6/20 22:03
     */
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public  User getUserById(@PathVariable int id){
        User user=userService.findOne(id);


        return  user;
    }

    @RequestMapping(value = "/search/{name}",method = RequestMethod.GET)
    public List<User> getUserByName(@PathVariable String name){
        return  userService.findByName(name);
    }

    @RequestMapping(value = "/update/{id}/{address}",method = RequestMethod.GET)
    public User updateUserByName(@PathVariable int id,@PathVariable String address){
        userService.updateUser(id,address);

        User user=userService.findOne(id);

        return  user;
    }
}
